#ifndef _CVUTIL_HPP
#define	_CVUTIL_HPP

#include <opencv/cv.h>

#include <iostream>
#include <vector>
using std::ostream;
using std::vector;


namespace cv {

const Scalar_<uchar> BLUE(255, 0, 0, 255);
const Scalar_<uchar> GREEN(0, 255, 0, 255);
const Scalar_<uchar> RED(0, 0, 255, 255);
const Scalar_<uchar> WHITE(255, 255, 255, 255);


typedef int64 timer;

void init_timer(timer& initial);

double seconds_spent(timer& initial);


Point2f rect_center(Rect rect);

double dist(Point2f& a, Point2f& b);

double dist2(Point2f& a, Point2f& b);

double line_angle(Point2f& orig, Point2f& dest);

Mat square_mask(Size mask_size, int square_side, Point center);


void findCornerSubPix(Mat& image, vector<Point2f>& corners,
                      Size window, Size zero_zone, CvTermCriteria criteria);

}

template<typename Pt>
cv::Point2i map_point(Pt pt, cv::Mat& img1, cv::Mat& img2)
{
  cv::Size size1 = img1.size(), size2 = img2.size();

  double scale_w = size2.width / double(size1.width);
  double scale_h = size2.height / double(size1.height);

  return cv::Point2i(pt.x * scale_w, pt.y * scale_h);
}

template<typename Pt>
ostream& operator<<(ostream& out, vector<Pt>& a)
{
  for (size_t i = 0; i < a.size(); ++i) {
    if (i) out << ' ';
    out << '(' << a[i].x << ',' << a[i].y << ')';
  }

  return out;
}

template<typename Pt>
bool operator<(const Pt& a, const Pt& b)
{
  if (a.x != b.x)
    return a.x < b.x;
  else
    return a.y < b.y;
}


#endif	/* _CVUTIL_HPP */
